package com.sky.controller.admin;

import com.sky.properties.AliyunOSSProperties;
import com.sky.result.Result;
import com.sky.utils.AliyunOSSUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;



@RestController
@Slf4j
@Api(tags = "文件上传下载接口")
public class UploadController {
    @Autowired
    private AliyunOSSProperties aliyunOSSProperties;

    @PostMapping("/admin/common/upload")
    public Result upload(MultipartFile file) throws Exception {
      log.info("文件上传：{}", file.toString());
      //获取字节数组
      byte[] content = file.getBytes();
      //获取文件后缀名
      String originalFilename = file.getOriginalFilename();
      String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
      //调用阿里云工具类上传方法并返回文件访问的url
      String url = AliyunOSSUtils.upload(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getBucketName(), content, extName);
      return Result.success(url);
    }

}
